package com.tsmti.system.entity;

import com.tsmti.core.common.constant.Constants;
import com.tsmti.core.common.entity.BaseDataEntity;
import com.tsmti.core.common.entity.BaseTreeNode;
import org.springframework.util.StringUtils;
import org.hibernate.annotations.*;
import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

/**
 * Title：PimsSystem
 * Class：com.ts.sys.entity
 * Comapny:
 *
 * @author：zcw
 * @version：2.5 CreateTime：2018/1/17 10:38
 * Modify log:
 * Description：
 */
@Entity
@Table(name = "pims_organization")
@DynamicUpdate
@DynamicInsert
@SuppressWarnings("serial")
@Where(clause = "delFlag = 0")
public class Organization extends BaseDataEntity<Long> implements BaseTreeNode<Long> {
    /**
     * 上级机构
     */
    private Organization parent;
    /**
     * 备注
     */
    private String remarks;
    /**
     * 是否包含子节点
     */
    private boolean hasChildren;
    /**
     * ID
     */
    private Long id;
    /**
     * 编号
     */
    private String code;
    /**
     * 机构名称
     */
    private String name;
    /**
     * 上级机构ID
     */
    private Long parentId;
    /**
     * 上级机构列表
     */
    private String parentIds;
    /**
     * 客户编码
     */
    private String customerNumber;

    private Boolean expanded = Boolean.FALSE;

    private Boolean loaded = Boolean.TRUE;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ORGANIZATION")
    @SequenceGenerator(name = "SEQ_ORGANIZATION", sequenceName = "organization_sequence", allocationSize = 1)
    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public Long getParentId() {
        return parentId;
    }

    @Override
    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    @Override
    public String getParentIds() {
        return parentIds;
    }

    @Override
    public void setParentIds(String parentIds) {
        this.parentIds = parentIds;
    }


    @Transient
    @Override
    public Boolean isRoot() {
        if (getParentId() == null || Constants.ZERO.equals(getParentId()) || Constants.EMPTY.equals(getParentId())) {
            return true;
        }
        return false;
    }

    /**
     * 获取上级 这里需要设置CascadeType.ALL，否则无法保存
     * @return
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name = "parentid", nullable = true, updatable = false, insertable = false)
    public Organization getParent() {
        return parent;
    }

    public void setParent(Organization parent) {
        this.parent = parent;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    /**
     * 是否有叶子节点
     */
    @Formula(value = "(select count(*) from pims_organization f_t where f_t.parentid = id)")
    public boolean getHasChildren() {
        return hasChildren;
    }

    public void setHasChildren(boolean hasChildren) {
        this.hasChildren = hasChildren;
    }


    @Transient
    public boolean getRoot() {
        if (getParentId() == null || Constants.ZERO.equals(getParentId()) || Constants.EMPTY.equals(getParentId())) {
            return true;
        }
        return false;
    }


    @Transient
    @Override
    public Long getLevel() {
        if (parentIds == null) {
            return (long) 0;
        }
        String[] parentIdArr = parentIds.split("/");
        List<String> idsList = new ArrayList<String>();
        for (String id : parentIdArr) {
            if (!StringUtils.isEmpty(id)) {
                idsList.add(id);
            }
        }
        return (long) (idsList.size());
    }

    @Transient
    @Override
    public Boolean isLeaf() {
        if (hasChildren) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @Transient
    public Boolean getLeaf() {
        if (getHasChildren()) {
            return Boolean.FALSE;
        }

        return Boolean.TRUE;
    }

    @Override
    public String makeSelfAsNewParentIds() {
        if (StringUtils.isEmpty(getParentIds())) {
            return getId() + getSeparator();
        }
        return getParentIds() + getId() + getSeparator();
    }

    @Transient
    @Override
    public String getSeparator() {
        return "/";
    }

    @Override
    public void setExpanded(Boolean expanded) {
        this.expanded = expanded;
    }

    @Transient
    @Override
    public Boolean getExpanded() {
        return expanded;
    }

    @Override
    public void setLoaded(Boolean loaded) {
        this.loaded = loaded;
    }

    @Transient
    @Override
    public Boolean getLoaded() {
        return loaded;
    }

    public String getCustomerNumber() {
        return customerNumber;
    }

    public void setCustomerNumber(String customerNumber) {
        this.customerNumber = customerNumber;
    }
}
